<?php

class ReservationController extends Controller
{
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl',
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array('create','cancel','print'),
				'users'=>array('@'),
			),
			array('allow',
				'actions'=>array(),
				'users'=>array('admin'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Creates a reservation.
	 * @param integer $itemId the item id.
	 * @param integer $quantity the quantity.
	 */
	public function actionCreate($itemId,$quantity)
	{
		$reservation = new Reservation();
		$reservation->itemId = $itemId;
		$reservation->ownerId = Yii::app()->user->id;
		$reservation->quantity = $quantity;
		$reservation->save();

		$this->redirect(array('//item/catalog'));
	}

	/**
	 * Cancels a reservation.
	 * @param integer $id the reservation id.
	 */
	public function actionCancel($id)
	{
		$model=Reservation::model()->findByPk($id);

		if($model!==null)
			$model->delete();

		$this->redirect(array('//item/catalog'));
	}

	/**
	 * Prints the logged in users reservations.
	 */
	public function actionPrint()
	{
		$this->layout='print';

		$dataProvider=new CActiveDataProvider('Reservation', array(
			'criteria'=>array(
				'condition'=>'ownerId=:userId',
				'params'=>array(':userId'=>Yii::app()->user->id),
			),
		));

		$this->render('print',array(
			'dataProvider'=>$dataProvider,
		));
	}
}